import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:juming_test/model/home/home_notice_model.dart';

class HomeNotice extends StatelessWidget {
  const HomeNotice({super.key, required this.noticeList});
  // 公告栏数据
  final List<List<HomeNoticeModel>> noticeList;

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Container(
        margin: EdgeInsets.all(16.w),
        decoration: BoxDecoration(
          border: Border.all(color: Colors.grey[300]!),
          borderRadius: BorderRadius.circular(5.w),
        ),
        width: double.infinity,
        height: 80.w,
        child: _buildPageView(),
      ),
    );
  }

  // 构建公告栏的PageView
  Widget _buildPageView() {
    return CarouselSlider(
      items:
          noticeList.map((item) {
            return _buildPage(item);
          }).toList(),
      options: CarouselOptions(
        autoPlay: true,
        viewportFraction: 1,
        scrollDirection: Axis.vertical,
      ),
    );
  }

  // 构建每一页的公告内容
  Widget _buildPage(List<HomeNoticeModel> notices) {
    return Column(
      children: notices.map((notice) => _buildNoticeItem(notice)).toList(),
    );
  }

  // 构建单个公告项
  Widget _buildNoticeItem(HomeNoticeModel notice) {
    return Expanded(
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 4.h, horizontal: 16.w),
        child: Row(
          children: [
            Container(
              width: 40.w,
              height: 20.h,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(6),
                color: notice.type.color,
              ),
              child: Center(
                child: Text(
                  notice.type.label,
                  style: TextStyle(fontSize: 10.sp, color: Colors.white),
                ),
              ),
            ),
            SizedBox(width: 8.w),
            Expanded(
              child: Text(
                notice.content,
                style: TextStyle(fontSize: 12.sp),
                overflow: TextOverflow.ellipsis,
              ),
            ),
            SizedBox(width: 8.w),
            Text(
              notice.updateTime,
              style: TextStyle(fontSize: 10.sp, color: Colors.grey[600]),
            ),
          ],
        ),
      ),
    );
  }
}
